home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / color.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  9KB  |  317 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  COLOR.H - Color Model Classes Include File
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/11/17 - Revised ColorRGB::SetColor
  9. //                         comments.
  10. //              94/11/26 - Added Spectra::Limit and
  11. //                         Spectra::Mult functions.
  12. //              94/12/16 - Version 1.01A release.
  13. //              95/02/05 - Version 1.02A release.
  14. //              95/06/13 - Added IntRGB class.
  15. //                       - Added CO_INT_SHIFT, CO_INT_MULT
  16. //                         and CO_MONO_SHIFT definitions.
  17. //                       - Changed C_BlueWeight, C_RedWeight
  18. //                         and C_GreenWeight constants from
  19. //                         floating point to integer data
  20. //                         type.
  21. //                       - Modified SetMono and SetPseudo
  22. //                         functions to modify RGB class
  23. //                         members.
  24. //              95/07/20 - Revised grayscale color weights.
  25. //              95/07/21 - Version 1.02B release.
  26. //              95/10/10 - Changed CO_INT_MULT definition
  27. //                         from INT_MAX to SHRT_MAX.
  28. //              96/02/14 - Version 1.02C release.
  29. //              96/04/01 - Version 1.03A release.
  30. //
  31. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  32. //              Borland C++ Version 4.5
  33. //
  34. //  Author:     Ian Ashdown, P.Eng.
  35. //              byHeart Software Limited
  36. //              620 Ballantree Road
  37. //              West Vancouver, B.C.
  38. //              Canada V7S 1W3
  39. //              Tel. (604) 922-6148
  40. //              Fax. (604) 987-7621
  41. //
  42. //  Copyright 1994-1996 byHeart Software Limited
  43. //
  44. //  The following source code has been derived from:
  45. //
  46. //    Ashdown, I. 1994. Radiosity: A Programmer's
  47. //    Perspective. New York, NY: John Wiley & Sons.
  48. //
  49. //  It may be freely copied, redistributed, and/or modified
  50. //  for personal use ONLY, as long as the copyright notice
  51. //  is included with all source code files.
  52. //
  53. ////////////////////////////////////////////////////////////
  54.  
  55. #ifndef _COLOR_H
  56. #define _COLOR_H
  57.  
  58. #include <limits.h>
  59. #include "general.h"
  60.  
  61. // Grayscale color weights
  62. //
  63. // NOTE: these weights are equivalent to 0.2125 (red),
  64. //       0.7154 (green), and 0.0721 (blue) in the floating
  65. //       point range of [0.0, 1.0].
  66. //
  67. //       These weights are specified in ITU-R Recommendation
  68. //       BT.709, "Basic Parameter Values for the Studio and
  69. //       for International Programme Exchange (1990),
  70. //       [formerly CCIR Rec. 709], ITU, 1211 Geneva 20,
  71. //       Switzerland. They are representative of the color
  72. //       phosphors used in contemporary CRTs.
  73.  
  74. static const int C_RedWeight = 54;
  75. static const int C_GreenWeight = 183;
  76. static const int C_BlueWeight = 18;
  77.  
  78. // Grayscale color shift value
  79. static const int CO_MONO_SHIFT = 8;
  80.  
  81. // Integer color multiplier
  82. static const float CO_INT_MULT = (float) SHRT_MAX;
  83.  
  84. // Integer color shift value
  85. static const int CO_INT_SHIFT = 7;
  86.  
  87. class Spectra   // Average spectral radiant exitance
  88. {
  89.   private:
  90.     float red_band;
  91.     float green_band;
  92.     float blue_band;
  93.  
  94.   public:
  95.     float GetBlueBand() { return blue_band; }
  96.     float GetGreenBand() { return green_band; }
  97.     float GetRedBand() { return red_band; }
  98.     void Limit()
  99.     {
  100.       red_band = max((float) 0.0, red_band);
  101.       green_band = max((float) 0.0, green_band);
  102.       blue_band = max((float) 0.0, blue_band);
  103.     }
  104.     void Reset()
  105.     { red_band = green_band = blue_band = (float) 0.0; }
  106.     void SetBlueBand( float b ) { blue_band = b; }
  107.     void SetGreenBand( float g ) { green_band = g; }
  108.     void SetRedBand( float r ) { red_band = r; }
  109.  
  110.     Spectra &Add( Spectra &a )  // Add color
  111.     {
  112.       red_band += a.red_band;
  113.       green_band += a.green_band;
  114.       blue_band += a.blue_band;
  115.  
  116.       return *this;
  117.     }
  118.  
  119.     Spectra &Subtract( Spectra &a )     // Subtract color
  120.     {
  121.       red_band -= a.red_band;
  122.       green_band -= a.green_band;
  123.       blue_band -= a.blue_band;
  124.  
  125.       return *this;
  126.     }
  127.  
  128.  
  129.     Spectra &Mult( Spectra &m )    // Multiply color
  130.     {
  131.       red_band *= m.red_band;
  132.       green_band *= m.green_band;
  133.       blue_band *= m.blue_band;
  134.  
  135.       return *this;
  136.     }
  137.  
  138.     // Multiply colors
  139.     friend Spectra Mult( Spectra &s1, Spectra &s2 )
  140.     {
  141.       Spectra temp;     // Temporary spectrum
  142.  
  143.       temp.red_band = s1.red_band * s2.red_band;
  144.       temp.green_band = s1.green_band * s2.green_band;
  145.       temp.blue_band = s1.blue_band * s2.blue_band;
  146.  
  147.       return temp;
  148.     }
  149.  
  150.     // Blend colors
  151.     friend Spectra Blend( Spectra &s1, Spectra &s2, double
  152.          alpha )
  153.     {
  154.       Spectra temp;     // Temporary spectrum
  155.  
  156.       // Linear interpolation
  157.       temp.red_band = s1.red_band + (s2.red_band -
  158.           s1.red_band) * (float) alpha;
  159.       temp.green_band = s1.green_band + (s2.green_band -
  160.           s1.green_band) * (float) alpha;
  161.       temp.blue_band = s1.blue_band + (s2.blue_band -
  162.           s1.blue_band) * (float) alpha;
  163.  
  164.       return temp;
  165.     }
  166.  
  167.     double GetMaxColor()        // Get maximum color
  168.     {
  169.       float maximum = (float) 0.0;
  170.  
  171.       maximum = max(maximum, red_band);
  172.       maximum = max(maximum, green_band);
  173.       maximum = max(maximum, blue_band);
  174.  
  175.       return (double) maximum;
  176.     }
  177.  
  178.     void Scale( double value )  // Scale color
  179.     {
  180.       red_band *= (float) value;
  181.       green_band *= (float) value;
  182.       blue_band *= (float) value;
  183.     }
  184. };
  185.  
  186. class ColorRGB  // 24-bit RGB color model
  187. {
  188.   private:
  189.     BYTE red;
  190.     BYTE green;
  191.     BYTE blue;
  192.  
  193.   public:
  194.     BYTE GetBlue() { return blue; }
  195.     BYTE GetGreen() { return green; }
  196.     BYTE GetRed() { return red; }
  197.     void SetBlue( BYTE b ) { blue = b; }
  198.     void SetGreen( BYTE g ) { green = g; }
  199.     void SetRed( BYTE r ) { red = r; }
  200.  
  201.     // Set 24-bit RGB color
  202.     void SetColor( Spectra &c )
  203.     {
  204.       red = (BYTE) (c.GetRedBand() * (float) UCHAR_MAX);
  205.       green = (BYTE) (c.GetGreenBand() * (float) UCHAR_MAX);
  206.       blue = (BYTE) (c.GetBlueBand() * (float) UCHAR_MAX);
  207.     }
  208.  
  209.     // Change 24-bit RGB color to grayscale
  210.     void SetMono()
  211.     {
  212.       red = green = blue = (BYTE) (((int) red * C_RedWeight
  213.           + (int) green * C_GreenWeight + (int) blue *
  214.           C_BlueWeight) >> CO_MONO_SHIFT);
  215.     }
  216.  
  217.     // Change 24-bit RGB color to pseudocolor
  218.     void SetPseudo()
  219.     {
  220.       int gsv;          // Grayscale value
  221.  
  222.       SetMono();        // Change color to grayscale
  223.       gsv = (int) red;  // Get grayscale value
  224.  
  225.       // Convert grayscale to pseudocolor
  226.       if (gsv < 128)
  227.       {
  228.         red = (BYTE) 0;
  229.         green = (BYTE) (2 * gsv);
  230.         blue = (BYTE) (UCHAR_MAX - 2 * gsv);
  231.       }
  232.       else
  233.       {
  234.         red = (BYTE) (2 * gsv - UCHAR_MAX);
  235.         green = (BYTE) (2 * UCHAR_MAX - 2 * gsv);
  236.         blue = (BYTE) 0;
  237.       }
  238.     }
  239. };
  240.  
  241. class IntRGB        // Integer RGB color
  242. {
  243.   private:
  244.     int red;
  245.     int green;
  246.     int blue;
  247.  
  248.   public:
  249.     int GetBlue() { return blue; }
  250.     int GetGreen() { return green; }
  251.     int GetRed() { return red; }
  252.     void AddBlue( int b ) { blue += b; }
  253.     void AddGreen( int g ) { green += g; }
  254.     void AddRed( int r ) { red += r; }
  255.     void SetBlue( int b ) { blue = b; }
  256.     void SetGreen( int g ) { green = g; }
  257.     void SetRed( int r ) { red = r; }
  258.  
  259.     // Set integer RGB color
  260.     void SetColor( Spectra &c )
  261.     {
  262.       red = (int) (c.GetRedBand() * (float)
  263.           CO_INT_MULT);
  264.       green = (int) (c.GetGreenBand() * (float)
  265.           CO_INT_MULT);
  266.       blue = (int) (c.GetBlueBand() * (float)
  267.           CO_INT_MULT);
  268.     }
  269.  
  270.     // Set 24-bit RGB color
  271.     void SetColorRGB( ColorRGB *pc )
  272.     {
  273.       pc->SetRed((BYTE) (red >> CO_INT_SHIFT));
  274.       pc->SetGreen((BYTE) (green >> CO_INT_SHIFT));
  275.       pc->SetBlue((BYTE) (blue >> CO_INT_SHIFT));
  276.     }
  277.  
  278.     // Subtract integer RGB color
  279.     friend IntRGB operator-( IntRGB &a, IntRGB &b )
  280.     {
  281.       IntRGB temp;      // Temporary integer RGB color
  282.  
  283.       temp.red = a.red - b.red;
  284.       temp.green = a.green - b.green;
  285.       temp.blue = a.blue - b.blue;
  286.  
  287.       return temp;
  288.     }
  289.  
  290.     // Subtract constant from integer RGB color
  291.     friend IntRGB operator-( IntRGB &a, int c )
  292.     {
  293.       IntRGB temp;      // Temporary integer RGB color
  294.  
  295.       temp.red = a.red - c;
  296.       temp.green = a.green - c;
  297.       temp.blue = a.blue - c;
  298.  
  299.       return temp;
  300.     }
  301.  
  302.     // Multiply integer RGB color by constant
  303.     friend IntRGB operator*( IntRGB &a, int c )
  304.     {
  305.       IntRGB temp;      // Temporary integer RGB color
  306.       
  307.       temp.red = a.red * c;
  308.       temp.green = a.green * c;
  309.       temp.blue = a.blue * c;
  310.  
  311.       return temp;
  312.     }
  313. };
  314.  
  315. #endif
  316.  
  317.